package com.baizhi.service.impl;

import com.baizhi.dao.CmfzUserDao;
import com.baizhi.entity.CmfzUser;
import com.baizhi.service.CmfzUserService;
import com.baizhi.utils.ThreadPollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Service
@Transactional
public class CmfzUserServiceImpl implements CmfzUserService {

    @Autowired
    private CmfzUserDao cud;
    @Override
    @Transactional(readOnly = true)
    public Page<CmfzUser> allUser(int pageNo, int pageSize) {
        Page<CmfzUser> page=new Page<>(pageNo,pageSize);
        Page<CmfzUser> cmfzUserPage = cud.selectPage(page, null);

        return cmfzUserPage;
    }

    @Override
    public void update(CmfzUser cmfzUser) {
            cud.updateById(cmfzUser);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<CmfzUser> selectByNameOrPhone(String name, String phone,int pageNo,int pageSize) {
        QueryWrapper<CmfzUser> cmfzUserQueryWrapper = new QueryWrapper<>();
        cmfzUserQueryWrapper.like("user_name", name).or().like("user_telphone", phone);
        Page<CmfzUser> cmfzUserPage = new Page<>(pageNo,pageSize);
        Page<CmfzUser> cmfzUserPage1 = cud.selectPage(cmfzUserPage, cmfzUserQueryWrapper);


        return cmfzUserPage1;
    }

    @Override
    public Map tableMaps() throws InterruptedException {
        Map map=new HashMap();

        CountDownLatch countDownLatch = new CountDownLatch(3);
        ThreadPoolExecutor executor = ThreadPollUtil.executor;
        executor.submit(new Runnable() {
            @Override
            public void run() {
                map.put("sex",cud.selBySexCount()) ;
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {

                map.put("address",cud.addressCount());
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
                map.put("weeks",cud.threeWeeks());
                countDownLatch.countDown();
            }
        });

        countDownLatch.await();



        return map;
    }

    @Override
    public Map tableTest() {
        Map map=new HashMap();
        map.put("sex",cud.selBySexCount()) ;
        map.put("address",cud.addressCount());
        map.put("weeks",cud.threeWeeks());
        return map;
    }
}
